package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class PathMeasureView extends View {


    private Paint paint;
    private Path path;
    private final String TAG = PathMeasureView.class.getSimpleName();
    private PathMeasure pathMeasure1;
    private PathMeasure pathMeasure2;
    private Path dst;

    public PathMeasureView(Context context) {
        super(context);
        init();
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //设置画笔的基本属性
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.RED);
        //设置填充样式
        paint.setStyle(Paint.Style.STROKE);
        //设置画笔宽度
        paint.setStrokeWidth(5);

        path = new Path();

        dst = new Path();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.translate(50, 50);
//        //设置画布变黑
//        canvas.drawColor(0xff000000);
//
//        //设置起点
//        path.moveTo(0, 0);
//        path.lineTo(0, 100);
//        path.lineTo(100, 100);
//        path.lineTo(100, 0);
//
//        canvas.drawPath(path, paint);
//
//        pathMeasure1 = new PathMeasure();
//        pathMeasure1.setPath(path, false);
//
//        pathMeasure2 = new PathMeasure(path, true);
//
//        Log.i(TAG, "onDraw: pathMeasure1= " + pathMeasure1.getLength() + " pathMeasure2= " + pathMeasure2.getLength());

//        canvas.translate(150, 150);
//        path.addRect(-50, -50, 50, 50, Path.Direction.CW);
//        canvas.drawPath(path, paint);
//        path.addRect(-100, -100, 100, 100, Path.Direction.CW);
//        canvas.drawPath(path, paint);
//        path.addRect(-120, -120, 120, 120, Path.Direction.CW);
//        canvas.drawPath(path, paint);
//
//        PathMeasure pathMeasure = new PathMeasure(path, false);
//        do {
//            float length = pathMeasure.getLength();
//            Log.i(TAG, "onDraw:  length = " + length);
//        } while (pathMeasure.nextContour());


        canvas.translate(100, 100);
        path.addRect(-50, -50, 50, 50, Path.Direction.CW);
//        canvas.drawPath(path, paint);


        PathMeasure pathMeasure = new PathMeasure(path, false);

        dst.lineTo(0, 100);

        pathMeasure.getSegment(0, 150, dst, false);

        canvas.drawPath(dst, paint);

    }
}
